/*
 * Copyright 2008 - 2009 Lars Heuer (heuer[at]semagia.com). All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.semagia.atomico.server.impl.jaxrs;

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;

import com.semagia.atomico.MediaType;
import com.semagia.atomico.dm.IWritableRepresentation;
import com.semagia.atomico.server.dm.ICollectionInfo;
import com.semagia.atomico.server.dm.IFragmentInfo;
import com.semagia.atomico.server.storage.StorageException;

/**
 * Represents a fragment.
 * 
 * @author Lars Heuer (heuer[at]semagia.com) <a href="http://www.semagia.com/">Semagia</a>
 */
public final class FragmentResource extends AbstractBaseResource {

    private final ICollectionInfo _collInfo;
    private final IFragmentInfo _fragmentInfo;
    private final long _lastModification;

    FragmentResource(final Request req, final ICollectionInfo collInfo, final String fragment) throws StorageException {
        super(req);
        _collInfo = collInfo;
        _fragmentInfo = getStorage().getFragmentInfo(collInfo.getId(), fragment);
        ResponseUtils.ensureExists(_fragmentInfo);
        _lastModification = _fragmentInfo.getUpdated();
    }

    /* (non-Javadoc)
     * @see com.semagia.atomico.server.impl.jaxrs.AbstractBaseResource#lastModification()
     */
    @Override
    protected long lastModification() {
        return _lastModification;
    }

    /**
     * Returns the requested fragment.
     *
     * @param headers HTTP headers.
     * @return The requested fragment or an reponse which indicates a failure (i.e. Not Found)
     * @throws StorageException In case of an error.
     */
    @GET
    @Produces("*/*")
    public Response getFragment(@Context HttpHeaders headers) throws StorageException {
        final Response.ResponseBuilder builder = makeResponseBuilder();
        final MediaType mediaType = com.semagia.atomico.server.utils.MediaTypeUtils.getPreferredMediaType(_fragmentInfo.getMediaTypes(), 
                MediaTypeUtils.toAtomicoMediaType(headers.getAcceptableMediaTypes()));
        if (mediaType == null) {
            return ResponseUtils.notAcceptable(_fragmentInfo.getMediaTypes());
        }
        // Add Vary header
        builder.variants(MediaTypeUtils.asVariants(_fragmentInfo.getMediaTypes()));
        builder.type(MediaTypeUtils.toJaxRSMediaType(mediaType));
        return ResponseUtils.buildStreamingEntity(builder, getFragment(mediaType));
    }

    private IWritableRepresentation getFragment(final MediaType mediaType) throws StorageException {
        return getStorage().getFragment(_collInfo.getCollectionId(),
                _fragmentInfo.getFragmentId(), mediaType);
    }

}
